package com.qdsg.ysg.doctor.ui;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.ui.adapter.AddMedicineAdapter;
import com.qdsg.ysg.doctor.ui.adapter.SecondAdapter;
import com.qdsg.ysg.doctor.ui.dialog.Finish3Dialog;
import com.qdsg.ysg.doctor.ui.dialog.MedicineDialog;
import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.MouldResponse;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

/**
 * 处方驳回详情
 * @time 2020/12/1 11:30
 */

public class AddRefusePrescriptionActivity extends BaseActivity {
    @BindView(R.id.edt_advice)
    TextView edt_advice;
    @BindView(R.id.edt_notice)
    EditText edt_notice;
    @BindView(R.id.tv_doctor)
    TextView tv_doctor;
    @BindView(R.id.pre)
    TextView pre;
    @BindView(R.id.main)
    TextView main;
    @BindView(R.id.tail)
    TextView tail;
    @BindView(R.id.tv_reason)
    TextView tv_reason;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.mould_container)
    RelativeLayout mould_container;
    @BindView(R.id.second_recyclerView)
    RecyclerView second_recyclerView;
    @BindView(R.id.tv_is_child)
    CheckBox tv_is_child;
    @BindView(R.id.btn_yuannei)
    RadioButton btn_yuannei;
    @BindView(R.id.btn_guoyao)
    RadioButton btn_guoyao;

    int type;
    AddMedicineAdapter adapter;
    SecondAdapter secondAdapter;
    List<PrescriptionResponse.Prescription> list = new ArrayList<>();
    List<PrescriptionResponse.Second> list2 = new ArrayList<>();
    String diagnoseId, prescriptionId, preAppendId, icdId, tailAppendId;

    MouldResponse.Mould mould;
    Finish3Dialog mFinish3Dialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_add_refuse_prescription;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( this );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        adapter = new AddMedicineAdapter( this, list );
        recyclerView.setAdapter( adapter );
        recyclerView.setNestedScrollingEnabled( false );
        recyclerView.setHasFixedSize( true );
        recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( AddRefusePrescriptionActivity.this, 1 );
            }
        } );
        LinearLayoutManager manager2 = new LinearLayoutManager( this );
        manager2.setOrientation( LinearLayoutManager.VERTICAL );
        second_recyclerView.setLayoutManager( manager2 );
        secondAdapter = new SecondAdapter( this, list2 );
        second_recyclerView.setAdapter( secondAdapter );
        second_recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( AddRefusePrescriptionActivity.this, 1 );
            }
        } );
        tv_is_child.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tv_is_child.setText( "是" );
                } else {
                    tv_is_child.setText( "否" );
                }
            }
        } );
    }

    @OnClick(R.id.btn_guoyao)
    void btn_guoyao() {
        if (type == 0) {
            if (list.size() > 0) {
                showFinish3Dialog( 0 );
            }
        }
    }

    @OnClick(R.id.btn_yuannei)
    void btn_yuannei() {
        if (type == 1) {
            if (list.size() > 0) {
                showFinish3Dialog( 1 );
            }
        }
    }


    private void showFinish3Dialog(final int type) {
        mFinish3Dialog = Finish3Dialog.newInstance( type );
        mFinish3Dialog.show( getSupportFragmentManager(), "Finish3Dialog" );
        mFinish3Dialog.setmListener( new Finish3Dialog.OnItemClickListener() {

            @Override
            public void onConfirm() {
                list.clear();
                adapter.notifyDataSetChanged();
                mFinish3Dialog.dismiss();
                if (type == 0) {
                    AddRefusePrescriptionActivity.this.type = 1;
                } else {
                    AddRefusePrescriptionActivity.this.type = 0;
                }
            }

            @Override
            public void onCancel() {
                if (type == 0) {
                    btn_yuannei.setChecked( true );
                } else {
                    btn_guoyao.setChecked( true );
                }
                mFinish3Dialog.dismiss();
            }
        } );
    }

    @Override
    protected void initData() {
        diagnoseId = getIntent().getStringExtra( "diagnoseId" );
        prescriptionId = getIntent().getStringExtra( "prescriptionId" );
        getDetail();
    }


    @OnClick(R.id.btn_add)
    void btn_add() {
        if (btn_yuannei.isChecked()) {
            showSelectHeadPicDialog( 0 );
        }
        if (btn_guoyao.isChecked()) {
            showSelectHeadPicDialog( 1 );
        }
    }
//    @OnClick(R.id.second_add)
//    void second_add() {
//        list2.add(new PrescriptionResponse().new Second());
//        secondAdapter.notifyDataSetChanged();
//    }

//    @OnClick(R.id.pre)
//    void pre() {
//        showDialog(2, 0);
//    }
//
//    @OnClick(R.id.tail)
//    void tail() {
//        showDialog(4, 0);
//    }
//
//    @OnClick(R.id.main)
//    void main() {
//        showSelectHeadPicDialog2(3, 0);
//    }


    @OnClick(R.id.mould_container)
    void mould_container() {
//        Bundle bundle = new Bundle();
//        bundle.putInt("type", 1);
//        startActivityForResult(MouldActivity.class, bundle, 1);
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        save();
    }

    private MedicineDialog mMedicineDialog = null;


    private void showSelectHeadPicDialog(int type) {
        // 初始化点击头像弹出的对话框
        mMedicineDialog = MedicineDialog.newInstance( type );
        mMedicineDialog.setOnItemClickListener( new MedicineDialog.OnItemClickListener() {
            @Override
            public void callback(PrescriptionResponse.Prescription p) {
                list.add( p );
                adapter.notifyDataSetChanged();
            }
        } );
        mMedicineDialog.show( getSupportFragmentManager(), "mMedicineDialog" );
    }

    private void save() {
        String isChildren = "";

        if (tv_is_child.isChecked()) {
            isChildren = "1";
        } else {
            isChildren = "0";
        }
        if (btn_yuannei.isChecked()) {
            type = 0;
        }
        if (btn_guoyao.isChecked()) {
            type = 1;
        }
        RestProxy.getInstance().savePrescription( diagnoseId, edt_advice.getText().toString(), edt_notice.getText().toString(), prescriptionId, preAppendId, icdId, tailAppendId, isChildren, type + "", list, list2, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                finish();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( AddRefusePrescriptionActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult( requestCode, resultCode, data );
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                mould = (MouldResponse.Mould) data.getSerializableExtra( "obj" );
                getInfo();
            }
        }
    }


    private void getInfo() {
        RestProxy.getInstance().getPrescriptionModel( mould.id, new Observer<MouldResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MouldResponse mouldResponse) {
                type = mouldResponse.data.type;
                if (mouldResponse.data.type == 0) {
                    btn_yuannei.setChecked( true );
                } else {
                    btn_guoyao.setChecked( true );
                }
                prescriptionId = mouldResponse.data.prescriptionId;
                edt_advice.setText( mouldResponse.data.advice );
                edt_notice.setText( mouldResponse.data.remark );
                list.clear();
                list.addAll( mouldResponse.data.westernList );
                adapter.notifyDataSetChanged();
                list2.clear();
                list2.addAll( mouldResponse.data.secondList );
                secondAdapter.notifyDataSetChanged();
                pre.setText( mouldResponse.data.preAppend );
                main.setText( mouldResponse.data.icd );
                tail.setText( mouldResponse.data.tailAppend );
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                ToastUtil.onError( AddRefusePrescriptionActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }


    private void getDetail() {
        RestProxy.getInstance().getPrescription( prescriptionId, new Observer<PrescriptionResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PrescriptionResponse prescriptionResponse) {
                type = prescriptionResponse.data.type;
                if (prescriptionResponse.data.type == 0) {
                    btn_yuannei.setChecked( true );
                } else {
                    btn_guoyao.setChecked( true );
                }
                list.clear();
                list.addAll( prescriptionResponse.data.westernList );
                adapter.notifyDataSetChanged();
                edt_advice.setText( prescriptionResponse.data.diagnoseResult );
                edt_notice.setText( prescriptionResponse.data.remark );
                tv_reason.setText( prescriptionResponse.data.reject );
                prescriptionId = prescriptionResponse.data.prescriptionId;
                tv_doctor.setText( prescriptionResponse.data.auditor );
                tv_reason.setText( prescriptionResponse.data.reject );
                list2.addAll( prescriptionResponse.data.secondList );
                secondAdapter.notifyDataSetChanged();
                list2.clear();
                list2.addAll( prescriptionResponse.data.secondList );
                secondAdapter.notifyDataSetChanged();
                pre.setText( prescriptionResponse.data.preAppend );
                main.setText( prescriptionResponse.data.icd );
                tail.setText( prescriptionResponse.data.tailAppend );
                if (prescriptionResponse.data.isChildren == 0) {
                    tv_is_child.setText( "否" );
                } else {
                    tv_is_child.setText( "是" );
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( AddRefusePrescriptionActivity.this, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }
}
